#! /usr/bin/perl
#
#  TOPPERS Software
#      Toyohashi Open Platform for Embedded Real-Time Systems
# 
#  Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
#                              Toyohashi Univ. of Technology, JAPAN
#  Copyright (C) 2004-2007 by Embedded and Real-Time Systems Laboratory
#              Graduate School of Information Science, Nagoya Univ., JAPAN
# 
#	The above copyright holders grant permission gratis to use,
#	duplicate, modify, or redistribute (hereafter called use) this
#	software (including the one made by modifying this software),
#	provided that the following four conditions (1) through (4) are
#	satisfied.
#
#	(1) When this software is used in the form of source code, the above
#    	copyright notice, this use conditions, and the disclaimer shown
#    	below must be retained in the source code without modification.
#
#	(2) When this software is redistributed in the forms usable for the
#    	development of other software, such as in library form, the above
#    	copyright notice, this use conditions, and the disclaimer shown
#    	below must be shown without modification in the document provided
#    	with the redistributed software, such as the user manual.
#
#	(3) When this software is redistributed in the forms unusable for the
#    	development of other software, such as the case when the software
#    	is embedded in a piece of equipment, either of the following two
#   	 conditions must be satisfied:
#
#		(a) The above copyright notice, this use conditions, and the
#         	disclaimer shown below must be shown without modification in
#     		the document provided with the redistributed software, such as
#      		the user manual.
#
# 		(b) How the software is to be redistributed must be reported to the
#     		TOPPERS Project according to the procedure described
#     		separately.
#
#	(4) The above copyright holders and the TOPPERS Project are exempt
#    	from responsibility for any type of damage directly or indirectly
#   	caused from the use of this software and are indemnified by any
#    	users or end users of this software from any and all causes of
#    	action whatsoever.
#
#	THIS SOFTWARE IS PROVIDED "AS IS." THE ABOVE COPYRIGHT HOLDERS AND
#	THE TOPPERS PROJECT DISCLAIM ANY EXPRESS OR IMPLIED WARRANTIES,
#	INCLUDING, BUT NOT LIMITED TO, ITS APPLICABILITY TO A PARTICULAR
#	PURPOSE. IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS AND THE
#	TOPPERS PROJECT BE LIABLE FOR ANY TYPE OF DAMAGE DIRECTLY OR
#	INDIRECTLY CAUSED FROM THE USE OF THIS SOFTWARE.
# 
#  @(#) $Id: applyrename 332 2007-07-17 12:15:37Z hiro $
# 

#
#  for file renaming
#
sub applyrename
{
	local($infile) = @_;
	local($outfile) = $infile.".new";
	local($line);

	open(INFILE, "< ".$infile) || die;
	open(OUTFILE, "> ".$outfile) || die;

	while ($line = <INFILE>) {
		$line =~ s/\b(_?)($syms)\b/$1_kernel_$2/gc;
		print OUTFILE $line;
	}

	close(INFILE);
	close(OUTFILE);

	if (`cmp $infile $outfile`) {
		rename($infile, $infile.".bak");
		rename($outfile, $infile);
		print STDERR "Modified: ",$infile,"\n";
	}
	else {
		unlink($outfile);
	}
}

#
#  error check
#
if ($#ARGV < 1) {
	die "Usage: appyrename <prefix> <filelist>\n";
}

#
#  initialize
#
@syms = ();
$name = $ARGV[0];

#
#  read symbol list
#
$deffile = $name."_rename.def";
open(INFILE, $deffile) || die "Cannot open $deffile";
while ($line = <INFILE>) {
	chomp $line;
	if ($line =~ /^#(.*)$/) {
		# do nothing
	}
	elsif ($line =~ /^INCLUDE[ \t]+(.*)$/) {
		# do nothing
	}
	elsif ($line ne "") {
		@syms = (@syms, $line)
	}
}
close(INFILE);

#
#  make string pattern for regular expression
#
$syms = join("|", @syms);

#
#  for file rename
#
shift @ARGV;
foreach $infile (@ARGV) {
	# if it's not file, skip
	next unless (-f $infile);

	do applyrename($infile) if ($infile ne $deffile);
}
